import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/models/report/selfReadingDto.dart';
import 'package:ichinesereader/utils/log.dart';
import '../../themes/appTheme.dart';
import 'package:ichinesereader/widgets/grids/selfReadingDataGrid.dart';

class SelfReadingRecords extends StatefulWidget {
  @override
  State<SelfReadingRecords> createState() => _SelfReadingRecordsState();
}

class _SelfReadingRecordsState extends State<SelfReadingRecords> {
  final box = Hive.box('appconfig');
  final ReportsController _reportsController = Get.find();
  bool isStudentOrIndividual = false;
  String _userName = '';

  @override
  void dispose() {
    super.dispose();
    if (_reportsController.selfReadingData.value.list != null)
      _reportsController.selfReadingData.value.list?.clear();
  }

  @override
  void initState() {
    isStudentOrIndividual = box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Individual ||
        box.get(CacheManagerKey['nanhairole'].toString()) ==
            Constants.NanhaiRole_Student;
    _userName = box.get(CacheManagerKey['username'].toString());

    if (isStudentOrIndividual) {
      SelfReadingDto dto = new SelfReadingDto(
          complete: true,
          start: DateTime.now().add(Duration(days: -15)).millisecondsSinceEpoch,
          end: DateTime.now().millisecondsSinceEpoch,
          onlyWithRecording: false,
          onlyWithWriting: false,
          progress: true);
      Future.delayed(Duration(milliseconds: 200), () {
        _reportsController.fetchSelfReading(_userName, dto);
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build student self reading Records');

    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(3.0),
          color: Get.isDarkMode
              ? AppTheme.colors.darkTheme2
              : Colors.white.withOpacity(0.3),
        ),
        child: Row(
          children: [
            Expanded(
              flex: 10,
              child: Container(
                  margin: EdgeInsets.all(12.0),
                  padding: EdgeInsets.all(
                    30,
                  ),
                  decoration: BoxDecoration(
                      color: Get.isDarkMode
                          ? AppTheme.colors.darkTheme2
                          : Colors.white,
                      borderRadius: BorderRadius.circular(3.0)),
                  child: _reportsController.obx(
                    (data) {
                      if (_reportsController.selfReadingData.value.list !=
                          null) {
                        if (_reportsController
                                .selfReadingData.value.list!.length ==
                            0) {
                          return Text('Data not available ...'.tr);
                        }
                        return SelfReadingDataGrid(
                            _reportsController.selfReadingData.value.list!,
                            _userName);
                      } else {
                        return Text("");
                      }
                    },
                  )),
            ),
          ],
        ),
      ),
    );
  }
}
